package cn.edu.zut.dao;

import cn.edu.zut.model.Reader;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository()
public class ReaderDaoImpl /*extends SqlSessionDaoSupport */implements ReaderDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    Reader reader;
    @Autowired
    List<Reader> readerList;
    /*
     * 解决在Spring3和Mybatis3整合的时候，dao层注入'sqlSessionFactory'或'sqlSessionTemplate'会报错,办法如下
     * 原因是因为Mybatis3依赖的jar位 mybatis-spring-1.2.0.jar,
     * 这个版本及以上的版本中对SqlSessionDaoSupport类中的'sqlSessionFactory'或'sqlSessionTemplate'注入方式进行了调整。
     * https://blog.csdn.net/zknxx/article/details/50959187
     */
//    @Autowired
//    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
//
//        super.setSqlSessionFactory(sqlSessionFactory);
//    }

    public int insertReader(Reader reader) {
        String INSERT_STUDENT="INSERT INTO reader(readerid, type, name, age, sex, phone, dept, regdate) VALUES(?,?,?,?,?,?,?,?)";
        Object[] args={reader.getReaderId(),reader.getType(),reader.getName(),reader.getAge(),reader.getSex(),reader.getPhone(),reader.getDept(),reader.getRegDate()};
        int result =jdbcTemplate.update(INSERT_STUDENT,args);
        if(result==1){
            return 1;
        }
        return 0;
    }

    public List<Reader> select_All_Reader() {
        String SELECT_ALL_READER="SELECT * FROM READER";
        RowMapper<Reader> readerRowMapper=new BeanPropertyRowMapper<Reader>(Reader.class);
        readerList=jdbcTemplate.query(SELECT_ALL_READER,readerRowMapper);
        return readerList;
    }

    public int updateReader(Reader reader) {
        String UPDATE_READER="update reader set readerid=?,type=?,name=?, age=?, sex=?, phone=?, dept=?, regdate=? where readerid=?";
        Object[] args={reader.getReaderId(),reader.getType(),reader.getName(),reader.getAge(),reader.getSex(),reader.getPhone(),reader.getDept(),reader.getRegDate(),reader.getReaderId()};
        int result=jdbcTemplate.update(UPDATE_READER,args);
        if(result==1){
            return 1;
        }
        return 0;
    }

    public int deleteReader(Reader reader) {
        String DELETE_READER="delete from reader where readerid=?";
        Object[] args={reader.getReaderId()};
        int result=jdbcTemplate.update(DELETE_READER,args);
        if(result==1){
            return 1;
        }
        return 0;
    }

    public Reader selectByReaderId(String readerId) {

        String SELECT_BY_ID="SELECT * FROM READER WHERE READERID=?";
        Object[] arg={readerId};
        RowMapper<Reader> rowMapper=new RowMapper<Reader>() {
            public Reader mapRow(ResultSet resultSet, int i) throws SQLException {
                Reader reader=new Reader();
                reader.setReaderId(resultSet.getString("readerid"));
                reader.setType(resultSet.getInt("type"));
                reader.setAge(resultSet.getInt("age"));
                reader.setName(resultSet.getString("name"));
                reader.setPhone(resultSet.getString("phone"));
                reader.setDept(resultSet.getString("dept"));
                reader.setRegDate(resultSet.getString("regdate"));
                reader.setSex(resultSet.getString("sex"));
                return reader;
            }
        };
        reader=(Reader) jdbcTemplate.queryForObject(SELECT_BY_ID,arg,rowMapper);
        /*用mybatis sqlsession实现*/
//        SqlSession sqlSession=this.getSqlSession();
//        Reader reader=sqlSession.selectOne("ReaderDao.selectByReaderId",readerId);
        return reader;
    }

    public Reader selectByReaderName(String name) {
        String SELECT_BY_NAME="SELECT * FROM READER WHERE name=?";
        Object[] arg={name};
        RowMapper<Reader> rowMapper=new RowMapper<Reader>() {
            public Reader mapRow(ResultSet resultSet, int i) throws SQLException {
                Reader reader=new Reader();
                reader.setReaderId(resultSet.getString("readerid"));
                reader.setType(resultSet.getInt("type"));
                reader.setAge(resultSet.getInt("age"));
                reader.setName(resultSet.getString("name"));
                reader.setPhone(resultSet.getString("phone"));
                reader.setDept(resultSet.getString("dept"));
                reader.setRegDate(resultSet.getString("regdate"));
                reader.setSex(resultSet.getString("sex"));
                return reader;
            }
        };
        reader=(Reader) jdbcTemplate.queryForObject(SELECT_BY_NAME,arg,rowMapper);
        return reader;
    }

    public List<Reader> selectByReaderDept(String dept) {
        String SELECT_BY_DEPT="SELECT * FROM READER WHERE dept=?";
        Object[] arg={dept};
        RowMapper<Reader> readerRowMapper=new BeanPropertyRowMapper<Reader>(Reader.class);
        readerList=jdbcTemplate.query(SELECT_BY_DEPT,arg,readerRowMapper);
        return readerList;
    }

    public List<Reader> selectByReaderType(int type) {
        String SELECT_BY_Type="SELECT * FROM READER WHERE type=?";
        Object[] arg={type};
        RowMapper<Reader> readerRowMapper=new BeanPropertyRowMapper<Reader>(Reader.class);
        readerList=jdbcTemplate.query(SELECT_BY_Type,arg,readerRowMapper);
        return readerList;
    }
}
